package main

import "fmt"

func testArray(){
  //var a []int
  //fmt.Println(a) //[]

  //var a [5]int
  //fmt.Println(a) //[0 0 0 0 0]

  var a [2]int
  a[0]=200
  a[1]=300
  fmt.Println(a) //[200 300]
}

func testArray2(){
  var a[5]int = [5]int{1,2,3,4,5}
  fmt.Println(a)
}

func testArray3(){
  a := [5]int{1,2,3,4,5}
  fmt.Println(a)
}

func testArray4(){
  a := [.../*←让编译器来推导数组的长度*/]int{1,2,3,4,5}
  fmt.Println(a)
}


//只初始化部分成员
func testArray5(){
  a := [5]int{1,2,3}
  fmt.Println(a) //[1 2 3 0 0]
}

func testArray6(){
  a := [5]int{3:100/*第4个元素(索引为3)为100*/, 4:300/*第5个元素为300*/}
  fmt.Println(a)
}


//func testArray7(){
//  a := [5]int{3:100, 4:300}
//  fmt.Println(a)
//
//  var b [3]int
//  b = a //Cannot use 'a' (type [5]int) as type [3]int
//}

func testArray8(){
  a := [5]int{3:100/*第4个元素(索引为3)为100*/, 4:300/*第5个元素为300*/}
  fmt.Printf("len(a)=%d\n",len(a))
}

//数组遍历方式一
func testArray9(){
  a := [5]int{3:100, 4:300}
  for i := 0; i < len(a); i++ {
    fmt.Printf("a[%d]=%d\n",i,a[i])
  }
}

//数组遍历方式二
func testArray10(){
  a := [5]int{3:100, 4:300}
  for index/*如果不需要,可以用_来表示忽略*/, value := range a {
    fmt.Printf("a[%d]=%d\n",index, value)
  }
}

//二维数组
func testArray11(){
  var a [3/*行*/][2/*列*/]int
  a[0][0] = 10
  a[0][1] = 20
  a[1][0] = 30
  a[1][1] = 30
  a[2][0] = 30
  a[2][1] = 30

  //fmt.Println(a) //[[10 20] [30 30] [30 30]]

  for i := 0; i < 3; i++ {
    for j := 0; j < 2; j++ {
      fmt.Printf("%d",a[i][j])
    }
    fmt.Println()
  }

  fmt.Println("another way")
  for i, val := range a {
    fmt.Printf("row[%d]=%v\n",i,val)
    for j, val2 := range val {
      fmt.Printf("(%d,%d)=%d",i,j,val2)
    }
  }
}
//二维数组
func testArray12(){
  //二维数组的初始化
  a := [3][2]string{
    {"lion","tiger"},
    {"cat","dog"},
    {"pigeon","peacock"},
  }

  printarray(a)
}
func printarray(a[3][2]string){
  for _, v1 := range a {
    for _, v2 := range v1 {
      fmt.Printf("%s",v2)
    }
    fmt.Printf("\n")
  }
}

func main() {
  //testArray()

  //testArray2()

  //testArray3()

  //testArray4() //[1 2 3 4 5]

  //testArray5() //[1 2 3 0 0]

  //testArray6() //[0 0 0 100 300]

  //testArray8() //len(a)=5

  //testArray9()

  //testArray10()

  //testArray11()
  testArray12()
}
